package com.guangfeng.yql.comm.netty.chatroom.messageImpl;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.guangfeng.yql.comm.netty.annotation.AChatRoomMessage;
import com.guangfeng.yql.comm.netty.chatroom.ChatRoomMessage;
import com.guangfeng.yql.comm.netty.enumerate.ChatRoomMessageType;
import com.guangfeng.yql.comm.netty.util.ChatRoomTextMessageSendUtil;
import com.guangfeng.yql.comm.netty.util.ChatRoomUtil;
import com.guangfeng.yql.comm.netty.util.NettyResponseBeanUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.springframework.stereotype.Component;

@AChatRoomMessage(ChatRoomMessageType.cdChatRoot)
@Component
public class CdChatRoom extends ChatRoomMessage {

    @Override
    public void tactics(ChannelHandlerContext ctx, TextWebSocketFrame msg) {
        JSON json = JSONUtil.parse(msg.text());
        Object path = json.getByPath("chatRoomId");
        if (path == null) {
            ChatRoomTextMessageSendUtil.sendMessage(NettyResponseBeanUtil.parameterError("直播间号不能为空"), ctx.channel());
        } else {
            try {
                ChatRoomUtil.cdChatRoom(ctx.channel(), Integer.valueOf(path.toString()));
            } catch (RuntimeException e) {
                e.printStackTrace();
                ChatRoomTextMessageSendUtil.sendMessage(NettyResponseBeanUtil.parameterError("直播间号不能为空"), ctx.channel());
            }

        }
    }
}
